package com.yiyi.credit.manage.sso.service.inner;

import com.yiyi.credit.manage.sso.cache.RedisManager;
import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.exception.ServiceException;
import com.yiyi.credit.manage.sso.mapper.SysResourceMapper;
import com.yiyi.credit.manage.sso.model.entity.SysResource;
import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;


@Service
public class SysResourceServiceImpl implements SysResourceService {

    @Autowired
    SysResourceMapper sysResourceMapper;

    @Autowired
    RedisManager redisManager;

    @Autowired
    SysRoleResourceService sysRoleResourceService;


    @Override
    public int addSysResource(SysResource sysResource) throws ServiceException {

        if (StringUtils.isBlank(sysResource.getName())) {
            throw new ServiceException(ResponseCode.RESOURCE_NAME_CANNOT_NULL);
        }

        if (StringUtils.isNotBlank(sysResource.getColumns())
                && (sysResource.getColumns().contains("\r\n")
                || sysResource.getColumns().contains("\r")
                || sysResource.getColumns().contains("\n"))) {

            throw new ServiceException(ResponseCode.INCLUDE_ERROR_STR);
        }

        sysResource.setCreateTime(new Date().getTime());
        sysResource.setUpdateTime(new Date().getTime());
        return sysResourceMapper.insertSelective(sysResource);
    }


    @Override
    public int updateSysResource(SysResource sysResource) throws ServiceException {
        if (StringUtils.isBlank(sysResource.getName())) {
            throw new ServiceException(ResponseCode.RESOURCE_NAME_CANNOT_NULL);
        }

        if (StringUtils.isNotBlank(sysResource.getColumns())
                && (sysResource.getColumns().contains("\r\n")
                || sysResource.getColumns().contains("\r")
                || sysResource.getColumns().contains("\n"))) {

            throw new ServiceException(ResponseCode.INCLUDE_ERROR_STR);
        }

        sysResource.setUpdateTime(new Date().getTime());
        return sysResourceMapper.  updateByPrimaryKeySelective(sysResource);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSysResourceById(Long id) throws ServiceException {
        //根节点不能删除
        SysResource rootSysResource = sysResourceMapper.selectByPrimaryKey(id);
        if (Constant.ROOT_PID_CODE.equals(rootSysResource.getParentId())) {
            throw new ServiceException(ResponseCode.ROOT_PID_CANNOT_DEL);
        }

        //判断当前节点是否包含子节点，如果包含则不能删除
        Example example = new Example(SysResource.class);
        example.createCriteria().andEqualTo("parentId", id);
        List<SysResource> list = sysResourceMapper.selectByExample(example);
        if (null != list && list.size() > Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.PID_CANNOT_DELETE);
        }
        //删除当前节点
        sysResourceMapper.deleteByPrimaryKey(id);

        //删除角色对应的资源
        sysRoleResourceService.deleteSysRoleResourceByResourceId(id);
    }


    @Override
    public SysResource getSysResourceById(Long id) throws ServiceException {

        return sysResourceMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<SysResource> getSysResourceBySysId(Long sysInfoId) throws ServiceException {

        Example example = new Example(SysResource.class);
        example.createCriteria().andEqualTo("sysInfoId", sysInfoId);
        return sysResourceMapper.selectByExample(example);
    }


    private void delSysResource(Long id) throws ServiceException {
        //删除角色资源中间表信息
        sysRoleResourceService.deleteSysRoleResourceByResourceId(id);
        //查询当前节点子节点
        Example example = new Example(SysResource.class);
        example.createCriteria().andEqualTo("parentId", id);
        List<SysResource> list = sysResourceMapper.selectByExample(example);
        if (null != list && list.size() > Constant.INT_ZERO) {
            for (SysResource sysResource : list) {
                sysResourceMapper.deleteByPrimaryKey(sysResource.getId());
                sysRoleResourceService.deleteSysRoleResourceByResourceId(sysResource.getId());
                delSysResource(sysResource.getId());
            }
        }
    }


    @Override
    public int deleteSysResourceBySid(Long sysInfoId) throws ServiceException {

        SysResource sysResource = new SysResource();
        sysResource.setSysInfoId(sysInfoId);
        return sysResourceMapper.delete(sysResource);
    }
}
